package com.atguigu.web;

import com.atguigu.pojo.Book;
import com.atguigu.pojo.Page;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BookServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数封装为bean对象
        Book book = WebUtils.copeParamToBean(req.getParameterMap(), new Book());
        //调用service中的的add方法
        bookService.addBook(book);
        //重定向到图书列表页面
//        req.getRequestDispatcher("/manager/bookServlet?action=list").forward(req,resp);
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }

    /**
     * 分页的方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的参数  pa'geNo pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //调用Bookservice中的 page（pageNo,pageSize）方法  返回Page对象
        Page<Book> page=bookService.page(pageNo,pageSize);
        //设置url请求地址
        page.setUrl("manager/bookServlet?action=page");
        //保存Page对象到request域中
        req.setAttribute("page",page);
        //强求转发到 /pages/manager/book_manager.jsp
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }

    /**
     * 根据id删除图书
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求删除的id数
        String id = req.getParameter("id");
        int i = WebUtils.parseInt(id, 0);
        //调用service中的delete方法
        bookService.deleteBookById(i);
        //重定向到  图书列表页面刷新参数
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }

    /**
     * 更新书的内容
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         //获取请求的参数封装为一个Book对象
        Book book = WebUtils.copeParamToBean(req.getParameterMap(), new Book());
        //调用service方法修改图书
        bookService.upadateBook(book);
        //重定向到图书列表
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }

    /**
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void geBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的图书编号

        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        //根据编号查询图书
        Book book = bookService.queryBookById(id);
        //保存到request域中
        req.setAttribute("book",book);
        //转发到pages/manager/book_edit.jsp
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过BookServiv类  获取所有的书籍对象
        List<Book> books = bookService.queryBooks();
        //把所有的书放在request域里面
        req.setAttribute("books", books);
        //请求转发到/pages/manage/book_manage.jsp
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);

    }
}
